// pages/detail/detail.js
const mta = require('../../../mta_sdk/mta_analysis.js');
var tool=require('../../../utils/util.js');
var ctx = null;
const app = getApp();
var school = ''
var classs = ''
var name = ''
var bgpic = ''
var pic = ''
var pid = ''
Page({

  data: {
    shareImg: '',
    shared: 0,
    ajaxSuccess:false
  },

  onLoad: function (opt) {
    mta.Page.init();
    console.log(opt)
    if (opt.shared == 1) {
      this.setData({
        shared: 1,
        shareImg: opt.shareImg || '',
        ajaxSuccess: true
      });

    }
    if (opt.name) {
      console.log("aaaa")
      this.makeImg(opt.name, opt.pic, opt.bgpic)

    }
    school = opt.school;
    classs = opt.class;
    name = opt.name;
    bgpic = opt.bgpic;
    pic = opt.pic;
    pid = opt.pid;
  },

  makeImg(name, pic, bgpic) {
    var me = this;
    if (me.data.shared == 1) {
      return false;
    }
    wx.showLoading({
      title: '加载中...',
    });
    
    
    var sysInfo = wx.getSystemInfoSync();
    var ft = sysInfo.windowWidth * .028;
    var query = wx.createSelectorQuery();
    ctx = wx.createCanvasContext("mycanvas");

   
    query.select('#canvas').boundingClientRect();//获取canvas的元素信息
    query.select('#headstyle').boundingClientRect();//获取canvas的元素信息
    query.exec(res => {
      var w = res[0].width;
      var h = res[0].height;
      var head_w = res[1].width;
      var head_h = res[1].height;

          wx.getImageInfo({
            src: pic,
            success: x => {

              ctx.drawImage(bgpic, 0, 0, w, h);

              ctx.drawImage(x.path, (w - head_w) / 4.7, (h - head_w) / 4.25, head_w, head_h);

              ctx.setFontSize(ft);
              ctx.fillStyle = '#222222';

              ctx.fillText(name, w / 3.7, h / 2);

              ctx.fillText(name, w / 1.32, h /6);

              ctx.fillText(Date.parse(new Date()), w / 4, h / 1.7);

              ctx.fillText('永久', w / 1.5, h / 1.8);

              ctx.fillText(tool.formatTime(new Date()), w / 1.5, h / 1.65);

              ctx.fillText(name, w / 1.5, h / 1.52);

              ctx.draw(false, function () {
                app.buldingImg("mycanvas", callBack => {
                  me.setData({
                    shareImg: callBack.data.url,
                    ajaxSuccess: true
                  });
                  wx.hideLoading();
                });
              });
            }
          })

    })

  },
  //保存图片
  saveImg() {
    app.savePic("mycanvas");
  },
  onShareAppMessage: function () {
    console.log(this.data.shareImg)
    return {
      title: '好儿子证书制作',
      desc: '好儿子证书制作',
      path: `/pages/${pid}/${pid}_detail/${pid}_detail?shared=1&shareImg=${this.data.shareImg}`,
      imageUrl: this.data.shareImg,
    }
  },
  onUnload() {
    wx.removeStorageSync("coverpic")
  },
  toPlay() {
    wx.navigateTo({
      url: `/pages/${pid}/${pid}_make/${pid}_make?pid=${pid}`,
    })
  },
  toHome() {
    wx.navigateTo({
      url: '/pages/index/index',
    })
  }
})